package cn.yes.jdbc;

import java.sql.*;
import java.util.Scanner;

public class AppLogin {
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入账号：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();
        loginplus(username,password);

    }
    public static void login(String username,String password) throws Exception{
        System.out.println("username="+username);
        System.out.println("password="+password);
        String sql="SELECT id FROM `user` WHERE username='" +username+
                "' AND `password`='" +password+ "'";
        System.out.println("组成的SQL语句是："+sql);
        Class.forName("com.mysql.jdbc.Driver");
        String usl="jdbc:mysql://localhost:43306/test";
        String name="root";
        String words="root";
        Connection connection = DriverManager.getConnection(usl, name, words);
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        if(resultSet.next()){
            System.out.println("登录成功");
        }
        else{
            System.out.println("登录失败");
        }

    }
    public static void loginplus(String username,String password) throws Exception{
        System.out.println("username="+username);
        System.out.println("password="+password);
        String sql="SELECT id FROM `user` WHERE username=? AND `password`=?";
        System.out.println("组成的SQL语句是："+sql);
        Class.forName("com.mysql.jdbc.Driver");
        String usl="jdbc:mysql://localhost:43306/test";
        String name="root";
        String words="root";
        Connection connection = DriverManager.getConnection(usl, name, words);
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2,password);
        ResultSet resultSet = ps.executeQuery();
        if(resultSet.next()){
            System.out.println("登录成功");
        }
        else{
            System.out.println("登录失败");
        }
    }
}
